import psutil
import os
import time




def exeIsActive(process_name):
    """
    判断"notepad++.exe"进程是否存在
    :return: 进程存在，返回False，否则为True
    """
    processes_name = []
    pids = psutil.pids()
    for pid in pids:
        p = psutil.Process(pid)
        processes_name.append(p.name())

    if process_name in processes_name:
        return True
    else:
        return False


def startProcess(processes_path):
    processes_name = str(processes_path).split("\\")[-1]
    # 打开软件
    if not exeIsActive(processes_name):
        os.startfile(processes_path)
        time.sleep(5)

    # # 模拟键盘输入ctrl+r
    # # ctrl键位码是17
    # win32api.keybd_event(17, 0, 0, 0)
    # time.sleep(1)
    # # r键位码是86
    # win32api.keybd_event(82, 0, 0, 0)
    # time.sleep(1)
    #
    # # 释放按键
    # win32api.keybd_event(82, 0, win32con.KEYEVENTF_KEYUP, 0)
    # time.sleep(1)
    # win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)


def closeProcess(processes_path):
    processes_name = str(processes_path).split("\\")[-1]
    if exeIsActive(processes_name):
        # 终止进程
        os.system(r'taskkill /F /IM {}'.format(processes_name))
        time.sleep(2)
